<template>
  <div class="main-header-box">
    <header class="main-header">
      <div class="container">
        <nav class="main-nav">
          <ul class="nav-list">
            <ul class="isResourceVisible">
              <li v-for="nav in navList" :key="nav.id" class="nav-item">
                <a :href="nav.path">{{ nav.label }}</a>
              </li>
            </ul>
            <ClientOnly>
              <ul class="right-side-nav">
                <li class="nav-item menu">
                  <div v-if="!globalStore.getUserInfo" class="login-button-wrapper">
                    <button class="login-button" @click="onLogInClick">登录</button>
                  </div>
                  <div v-else class="avatar-wrapper">
                    <van-button type="primary" @click="logout">退出登录</van-button>
                  </div>
                </li>
              </ul>
            </ClientOnly>
          </ul>
        </nav>
      </div>
    </header>
  </div>
</template>

<script lang="ts" setup>
import { getUserInfo } from '../api/lgoin.ts'
import { useGlobalStore } from '@/store'
const globalStore = useGlobalStore()
const token = useCookie('token')
const navList = [
  {
    id: 1,
    label: '首页',
    path: '/',
  },
  {
    id: 2,
    label: '彩票',
    path: '/game',
  },
  {
    id: 3,
    label: '体育',
  },
  {
    id: 4,
    label: '视讯',
  },
  {
    id: 5,
    label: '电子',
  },
  {
    id: 6,
    label: '棋牌',
  },
  {
    id: 7,
    label: '优惠活动',
  },
]

if (!globalStore.getUserInfo && token?.value) {
  const user = await getUserInfo()
  globalStore.setUserInfo(user?.data)
}

onMounted(() => {})

// 退出登录
const logout = () => {
  globalStore.setUserInfo(null)
  if (token?.value) {
    token.value = null
  }

  // 刷新页面
  location.reload()
}

const onLogInClick = () => {
  navigateTo('/login')
}
</script>

<style lang="scss" scoped>
.main-header-box {
  position: relative;
  height: 60px;
}

.main-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  //transition: all 0.2s;
  background: var(--navbar-background-color);
  border-bottom: 1px solid var(--border-line-color);
  color: var(--color-nav-title);
  height: 60px;
  z-index: 250;
  .container {
    margin: auto;
    height: 100%;
    max-width: 1440px;
    display: flex;
    align-items: center;
    .logo {
      margin-right: 12px;
      margin-left: 24px;
      display: inline-block;
      height: 22px;
      min-width: 107px;
      img {
        display: block;
        width: 100%;
        height: 22px;
      }
    }
    .main-nav {
      height: 100%;
      flex: 1 0 auto;
      .nav-list {
        height: 100%;
        display: flex;
        justify-content: space-between;
        .isResourceVisible {
          display: flex;
          .nav-item {
            font-size: 14px;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            position: relative;
            color: var(--primary-color);
            &:hover {
              color: var(--primary-color);
            }
            a {
              color: inherit;
              display: inline-block;
              height: 60px;
              padding: 0 12px;
              line-height: 60px;
            }
          }
        }
        .right-side-nav {
          display: flex;
          .nav-item {
            cursor: pointer;
            display: flex;
            align-items: center;
            padding: 0 12px;
          }
          .notification {
            fill: #8a919f;
            stroke: #8a919f;
            color: #8a919f;
            &:hover {
              fill: #515767;
              stroke: #515767;
              color: #515767;
            }
            svg {
              width: 24px;
              height: 24px;
            }
          }
          .menu {
            .login-button-wrapper {
              .login-button {
                cursor: pointer;
                background: rgba(30, 128, 255, 0.05);
                border: 1px solid rgba(30, 128, 255, 0.3);
                border-radius: 4px;
                padding: 4px 12px;
                color: var(--link-color);
                line-height: 22px;
                font-size: 14px;
                font-weight: 400;
                margin-right: 24px;
                height: 36px;
                overflow: hidden;
              }
            }
          }
        }
      }
    }
  }
}
</style>
